/**
 * 用于 图文 模块
 */
const Pager = require('../../components/aotoo/core/index')
const lib = Pager.lib

import { chunkx } from '../../utils/util'



const adapter = (data, type) => {
  const num = type == 1 ? 3 : 6
  let output = []
   data.map((item, ii) => {
     output.push({
      img:  {
        src: item.img,
        itemClass: 'item-pic',
        mode: item.mode || 'scaleToFill'
      },
      title: [
        {title: item.title, itemClass: 'item-title'},
        {title: item.desc, itemClass: 'item-desc'}
      ],
      aim: item.aim ? 'onJumpPages?path='+item.aim : '',
      itemClass: 'ss-imagetext-list-item-box-item'
    })
  })
  const chunkData = chunkx(output, num)
  let _newData = []
  chunkData.map(item => {
    _newData.push({
      li: item,
      liClass: 'ss-imagetext-list-item-box'
    })
  })
  return _newData
}

module.exports = function(params={}) {
  let dft = {
    type: 'swiper',
    num: 1,                 // 表示一个轮播有几个item，默认 1
  }
  let opts = Object.assign({}, dft, params)
  let type = opts.type == 'scroll' ? {
    is: 'scroll',
    'scroll-y': true
  } : opts.type == 'swiper' ? {
    is: 'swiper',
    'previous-margin': opts.config && opts.config['previous-margin'] || '24rpx',
    'next-margin': opts.config && opts.config['next-margin'] || '180rpx',
    'easing-function': 'easeInOutCubic',
    duration: '500'
  } : {}
  return {
    listClass: ('ss-imagetext-list-lr ' + opts.listClass),
    itemClass: ('ss-imagetext-list-item ' + opts.itemClass),
    type: type,
    data: adapter(opts.data, opts.num),
    itemMethod: {
      onJumpPages(e, params, inst) {
        const $path = params.patn
        wx.navigateTo({
          url: $path
        })
      }
    },
    methods: {
      __ready() {
        const that = this
        const xx = that.parentInst
        const yy = that.parent()
        setTimeout(() => {
          // console.log(xx, yy);
          let query = wx.createSelectorQuery().in(that)
          console.log(that, 'query');
          query.select('.ss-imagetext-list-item').boundingClientRect(ret => {
            console.log(ret)
            if (ret.height != 0) {
              that.setData({'$list.listStyle': `height: ${ret.height}px`})
            }
          }).exec()
        }, 0);
      }
    }
  }
}
